package com.tengju.bff.interfaces.manage.member.sign.blacklist.facade;

import com.google.common.collect.Lists;
import com.tengju.bff.interfaces.manage.member.sign.blacklist.form.GetStarBlacklistForm;
import com.tengju.bff.interfaces.manage.member.sign.blacklist.vo.PageStarBlacklistVO;
import com.tengju.bff.interfaces.manage.star.assembler.StarBlacklistAssembler;
import com.tengju.user.domain.model.sign.StarBlacklist;
import com.tengju.user.domain.model.sign.StarBlacklistRepository;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.service.UserDocService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class StarBlacklistFacade {

    private final StarBlacklistRepository starBlacklistRepository;
    private final UserDocService userDocService;

    public StarBlacklistFacade(UserDocService userDocService,
                               StarBlacklistRepository starBlacklistRepository) {
        this.userDocService = userDocService;
        this.starBlacklistRepository = starBlacklistRepository;
    }

    public PageStarBlacklistVO getStarBlackListByPage(GetStarBlacklistForm form) {

        Integer pageSize = form.getPageSize();
        Integer pageIndex = form.getPageIndex();
        int offset = (pageIndex - 1) * pageSize;
        UserIdCode idCode = Optional.ofNullable(form.getIdCode())
                .map(UserIdCode::new)
                .orElse(null);
        String mobile = form.getMobile();

        int total = 0;
        List<StarBlacklist> list = Lists.newArrayList();

        if (idCode != null || StringUtils.isNotBlank(mobile)) {
            UserInfo userInfo = userDocService.searchUserByIdCodeOrMobile(idCode, mobile);
            if (userInfo != null) {
                StarBlacklist starBlacklist = starBlacklistRepository
                        .getByIdCode(userInfo.getIdCode().getValue(), userInfo.getMobile());
                if (starBlacklist != null) {
                    total = 1;
                    list.add(starBlacklist);
                }
            }
            return StarBlacklistAssembler.ModelToVO(list, form, total);
        }

        total = starBlacklistRepository.count();

        if (offset <= total) {
            list = starBlacklistRepository.queryByPage(offset, pageSize);
            List<UserIdCode> userIdCodes = list.stream()
                    .map(StarBlacklist::getIdCode)
                    .collect(Collectors.toList());
            List<UserInfo> userInfos = userDocService.searchUserByIdCodes(userIdCodes);
            Map<UserIdCode, UserInfo> userInfoMap = userInfos.stream().collect(
                    Collectors.toMap(UserInfo::getIdCode, userInfo -> userInfo));
            list.forEach(starBlacklist -> {
                UserInfo userInfo = userInfoMap.get(starBlacklist.getIdCode());
                starBlacklist.setMobile(
                        Optional.ofNullable(userInfo).map(UserInfo::getMobile).orElse(null));
            });
        }
        return StarBlacklistAssembler.ModelToVO(list, form, total);
    }
}
